package com.ego.service.impl;

import com.ego.commons.utils.FastDFSClient;
import com.ego.service.PicService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
public class PicServiceImpl implements PicService {


    @Value("${ego.fastdfs.nginx}")
    private String fastdfsnginx;

    @Override
    public Map<String, Object> uploadFile(MultipartFile uploadFile) {


        Map<String,Object> map = new HashMap<>();

        try {
            //获取文件名称
            String filename = uploadFile.getOriginalFilename();//dog.jpg

            //获取文件输入流
            InputStream inputStream = uploadFile.getInputStream();

            String[] result = FastDFSClient.uploadFile(filename, inputStream);
            // ["group1","M00/00/00/wKh-FGAt4muAO-1HAAItIjfWc9U485.jpg"]
            System.out.println("Arrays.toString(result) = " + Arrays.toString(result));


            //返回图片地址
            //写死叫做硬编码
            //String imageUrl = "http://192.168.126.20:8888/"+result[0]+"/"+result[1];
            String imageUrl = fastdfsnginx+result[0]+"/"+result[1];
            map.put("error",0);
            map.put("url",imageUrl);

        } catch (IOException e) {
            e.printStackTrace();

            map.put("error",1);
            map.put("message","上传文件失败");

        }
        return map;
    }
}
